package com.shujia.wyh.day26;

public class GetThread2 implements Runnable {
    private Student2 student;
//    private Object obj = new Object();
    private Demo demo;

    GetThread2(Student2 student) {
        this.student = student;
    }

    GetThread2(Student2 student,Demo demo) {
        this.student = student;
        this.demo = demo;
    }

    @Override
    public void run() {
//        Student student = new Student();
        while (true){
            synchronized (demo){
                //先看看学生对象有没有值
                //当flag为false的时候，说明对象还没有生产数据，这时候消费者等待，等待生产者生产数据
                //当flag为true的时候，说明对象数据生产完毕，作为消费者就应该消费数据
                if(!student.flag){
                    try {
//                        student.wait();
                        demo.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //这一步相当于消费数据
                System.out.println(student.name + "---" + student.age);

                //通知生产者生产数据
//                student.notify();
                demo.notify();
                student.flag = false;
            }

        }
    }
}
